import { login } from '@/api/login/index';
import { setToken } from '@/utils/auth.js';
import router from '@/router';

export const autoLogin = function(self) {
  setTimeout(() => {
    const loading = self.$loading({
      lock: true,
      text: '正在登录中...',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    });

    const loginRequest = { 'password': '123456', 'username': '13758127433', 'year': 2021, 'name': '13758127433', 'appId': '161', 'browser': 'chrome', 'operateSystem': 'windows' };
    login(loginRequest).then(res => {
      dealLoginInfo(res, self);
      setTimeout(() => {
        loading.close();
      }, 500);
    });
  }, 600);
};

const dealLoginInfo = function(res, self) {
  const query = self.$route.query;
  res.data.roleIdList = res.data.roleIdList.filter((item, index) => index === 0);
  setToken('xcNickName', res.data.trueName);
  setToken('chooseYear', 2021);
  setToken('avatar', res.data.avatar);
  setToken('avatarId', res.data.avatarId);
  setToken('xcToken', res.token);
  setToken('isShowLoading', '1');
  setToken('xcUserId', res.data.id);
  setToken(
    'enterpriseList',
    JSON.stringify(res.data.enterprise)
  );
  setToken(
    'enterprise_jc',
    JSON.stringify(res.data.enterprise_jc)
  );

  const role = res.data.roleIdList[0].children[0];
  chooseRole(role, query);

  // self.chooseRole(res.data.roleIdList[0]);
  // console.log(res.data.roleIdList[0].children[0]);
};

const chooseRole = function(item, query) {
  setToken('xcRoleId', item.id);
  setToken('xcRoleName', item.name);
  setToken('isCanGoto', 'isCanGoto');
  setToken('cache', 'cqc');
  setToken('isHideTop', '1');
  if (item.pcUrlName === '/layoutContainer') {
    setToken('isHideLeft', 1);
    setToken('isHideTop', 1);
    setToken('isShowAllRoute', 1);
  }

  // console.log(query);
  router.push({
    path: query.path
  });
  setToken('pcUrlName', item.pcUrlName);
  setToken('pcUrl', item.pcUrl);
};
